package acl.bussiness;


import acl.hibernate.dao.ACLdao;
import acl.hibernate.po.OaPermissions;
import acl.hibernate.po.OaUsersRoles;
import acl.hibernate.po.OaRolesPermissions;
import acl.hibernate.po.OaDepartments;
import java.util.LinkedList;
import java.util.List;


public class ACLBussiness {
	ACLdao acldao = new ACLdao();

	public List getAllPermission() {// 获取所有的权限
		// author:Silence
		return acldao.getAllPermission();
	}

	public List getUserPermissions(Integer userId) {// 根据角色id获取角色权限
		// author:Never
		List<OaPermissions> result = new LinkedList<OaPermissions>();
		List permission = null;
		List userRoles = getUserRoles(userId);
		if (userRoles != null) {
			try {
				permission = getRolePermissions(((OaUsersRoles) userRoles
						.get(0)).getRoleId());
			} catch (Exception e) {
				System.out.println(e.toString());
				return null;
			}
			for (int i = 1; i < userRoles.size(); i++) {
				permission.addAll(getRolePermissions(((OaUsersRoles) userRoles
						.get(i)).getRoleId()));
			}

			// 删除相同权限
			for (int i = 0; i < permission.size() - 1; i++) {
				int b = ((OaRolesPermissions) (permission.get(i)))
					.getPermissionId();
				for (int j = permission.size() - 1; j > i; j--) {
					int a = ((OaRolesPermissions) (permission.get(j)))
							.getPermissionId();
					if (a == b) {
						permission.remove(j);
					}
				}
			}

			ACLBussiness aclbussiness = new ACLBussiness();
			List allPermission = aclbussiness.getAllPermission();// 所有的权限

			// 用户权限ID与所有权限对照，获取权限名称
			for (int i = 0; i < allPermission.size(); i++) {
				OaPermissions oaPermission = (OaPermissions) allPermission
						.get(i);
				int a = oaPermission.getId();
				for (int j = 0; j < permission.size(); j++) {
					int b = ((OaRolesPermissions) (permission.get(j)))
							.getPermissionId();
					if (a == b) {
						result.add(oaPermission);
						continue;
					}
				}
			}
		}
		return result;
	}
	public List getUserBigPermissions(Integer userId) {//根据角色id获取角色大类名称
		// author:Never
		List result=getUserPermissions(userId);
		if(result==null)
			return null;
		for(int i=result.size()-1;i>=0;i--){
			if(((OaPermissions) (result.get(i))).getIsLink()==0)
				result.remove(i);
		}
		for (int i = 0; i < result.size(); i++) {// 删除相同desctype
			for (int j = result.size()-1; j >i; j--) {
				String a = ((OaPermissions) (result.get(j))).getDesctype()
						.trim();
				String b = ((OaPermissions) (result.get(i))).getDesctype()
						.trim();
				if (a.equals(b)) {
					result.remove(j);
				}
			}
		}
		
		return result;
	}
	
	public List getUserBigPermissionsForRoles(Integer userId) {//根据角色id获取角色大类名称
		// author:Never
		List result=getUserPermissions(userId);
		for(int i=result.size()-1;i>=0;i--){
			if(((OaPermissions) (result.get(i))).getReplace()!=0)
				result.remove(i);
		}
		for (int i = 0; i < result.size(); i++) {// 删除相同desctype
			for (int j = result.size()-1; j >i; j--) {
				String a = ((OaPermissions) (result.get(j))).getDesctype()
						.trim();
				String b = ((OaPermissions) (result.get(i))).getDesctype()
						.trim();
				if (a.equals(b)) {
					result.remove(j);
				}
			}
		}
		
		return result;
	}
	public List getPanelForRoles(Integer userId) {//根据角色id获取panel
		// author:Never
		List result=getUserPermissions(userId);
		for(int i=result.size()-1;i>=0;i--){
			if(((OaPermissions) (result.get(i))).getReplace()!=0)
				result.remove(i);
		}
		for (int i = 0; i < result.size(); i++) {// 删除相同panel
			for (int j = result.size()-1; j > i; j--) {
				String a = ((OaPermissions) (result.get(j))).getPanel()
						.trim();
				String b = ((OaPermissions) (result.get(i))).getPanel()
						.trim();
				if (a.equals(b)) {
					result.remove(j);
				}
			}
		}
		return result;
	}
	
	public List getUserPanel(Integer userId) {//根据角色id获取panel
		// author:Never
		List result=getUserPermissions(userId);
		if(result==null)
			return result;
		for(int i=result.size()-1;i>=0;i--){
			if(((OaPermissions) (result.get(i))).getIsLink()==0)
				result.remove(i);
		}
		for (int i = 0; i < result.size(); i++) {// 删除相同panel
			for (int j = result.size()-1; j > i; j--) {
				String a = ((OaPermissions) (result.get(j))).getPanel()
						.trim();
				String b = ((OaPermissions) (result.get(i))).getPanel()
						.trim();
				if (a.equals(b)) {
					result.remove(j);
				}
			}
		}
		return result;
	}
	
	public List getRolePermissions(Integer roleId) {// 根据角色id获取角色权限
		// author:Never
		return acldao.getRolePermissions(roleId);
	}
	public List getAllPermission(Integer userId){
		//查询所有权限
		List result=getUserPermissions(userId);
		
		
		return result;
		}
	public List getSmallPermission(List l){
		return acldao.getSmallPermission(l);
	}
	public List getUserRoles(Integer userId) {// 根据用户id获取角色id
		// author:Never
		return acldao.getUserRoles(userId);
	}

	public OaDepartments getDepartment(Integer id) {
		// 根据部门id返回部门信息
		// author:Silence
		return acldao.getDepartmentbyid(id);
	}

}
